package com.whately.core.base.utils;

import com.whately.core.base.constant.PwdEncodeEnum;
import com.whately.core.base.exception.ServiceError;
import com.whately.core.base.exception.ServiceException;
import com.whately.core.base.utils.bcrypt.BCryptPasswordEncoderFactory;
import com.whately.core.base.vo.req.CheckModifyPwdCorrectReqVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@Slf4j
public class PwdUtil {

    public static void checkWhenModifyPwd(CheckModifyPwdCorrectReqVo requestVo){
        String oldPwd = requestVo.getPwd();
        String newPwd = requestVo.getNewPwd();
        String newPwdConfirm = requestVo.getNewPwdConfirm();

        if (StringUtils.isBlank(oldPwd)  ) {
            log.error("corpModifyPwd bad request parameter requestVo {}", requestVo);
            throw new ServiceException(ServiceError.REQUEST_PARAMETER_ERROR.getCode(),"旧密码不能为空");
        }

        if ( StringUtils.isBlank(newPwd) ) {
            log.error("corpModifyPwd bad request parameter requestVo {}", requestVo);
            throw new ServiceException(ServiceError.REQUEST_PARAMETER_ERROR.getCode(),"新密码不能为空");
        }

        if (!StringUtils.equals(newPwd, newPwdConfirm)) {
            throw new ServiceException(ServiceError.REQUEST_PARAMETER_ERROR.getCode(), "新密码与确认密码不一致");
        }

        if (StringUtils.equals(newPwd, oldPwd)) {
            throw new ServiceException(ServiceError.REQUEST_PARAMETER_ERROR.getCode(), "新旧密码不能相同");
        }
        PwdUtil.checkPwd(newPwd);
    }


    public static void checkPwd(String pwd) {
        String regexPw = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$";
        if (!pwd.matches(regexPw)) {
            throw new ServiceException(ServiceError.REQUEST_PARAMETER_ERROR.getCode(), "密码同时含有数字和字母，长度8-20之间, 区分大小写");
        }
    }

    /**
     * 对明文密码加密
     * @param rawPwd
     * @param pwdEncodeEnum
     * @return
     */
    public static String encode(String rawPwd, PwdEncodeEnum pwdEncodeEnum){
        if(pwdEncodeEnum==null|| StringUtils.isBlank(rawPwd)){
            throw new ServiceException(400,"密码或者加密方式错误");
        }
        if(pwdEncodeEnum==PwdEncodeEnum.bcrypt){
            return BCryptPasswordEncoderFactory.getBCryptPasswordEncoderUtil().encode(rawPwd);
        }else if(pwdEncodeEnum==PwdEncodeEnum.md5_twice){
            return MD5Util.md5(MD5Util.md5(rawPwd));
        }
        return null;
    }


    /**
     * 密码比对
     */
    public static boolean matches(String rawPwd, PwdEncodeEnum pwdEncodeEnum, String encodedPassword){
        if(pwdEncodeEnum==null|| StringUtils.isBlank(rawPwd)||StringUtils.isBlank(encodedPassword)){
            throw new ServiceException(400,"密码或者加密方式错误");
        }
        if(pwdEncodeEnum==PwdEncodeEnum.bcrypt){
            return BCryptPasswordEncoderFactory.getBCryptPasswordEncoderUtil().matches(rawPwd,encodedPassword);
        }else if(pwdEncodeEnum==PwdEncodeEnum.md5_twice){
            return encodedPassword.equals(MD5Util.md5(MD5Util.md5(rawPwd)));
        }
        return false;
    }
}
